package com.sense.cloud.sms.web.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @time 2016-05-25
 * @author 新歌<xingeking@163.com>
 * @email xingeking@163.com
 * @version v1.0
 * @function 首页管理
 * @info java spring aop
 * @update none
 */
@Controller
@Scope("prototype")
@RequestMapping("master")
public class IndexMasterController {

	@RequestMapping({ "", "index.do" })
	public String mindex() {
		List<Menu> menus = getMenus(getRess(), 1, "0");
		getSession().setAttribute("a", "a");
		getSession().setAttribute("menus", menus);
		return "/master/index";
	}

	// 菜单显示
	@ResponseBody
	@RequestMapping("setMenu.do")
	public void setMenu(@RequestParam(value = "lv1", required = false) String lv1,
			@RequestParam(value = "lv2", required = false) String lv2,
			@RequestParam(value = "lv3", required = false) String lv3) {
		getSession().setAttribute("lv1", 1);
		if (isNotEmpty(lv1)) {
			getSession().setAttribute("lv1", lv1);
		}
		if (isNotEmpty(lv2)) {
			getSession().setAttribute("lv2", lv2);
		}
		if (isNotEmpty(lv3)) {
			getSession().setAttribute("lv3", lv3);
		}
	}

	public List<Menu> getMenus(List<Ress> nowRess, Integer level, String pid) {
		List<Menu> menus = new ArrayList<Menu>();
		for (Ress res : nowRess) {
			if (null != res && (level == res.getResLevel()) && (pid.equals(res.getResPid()))) {
				Menu m = new Menu(res.getResName(), res.getResUrl(), res.getResSort(), res.getResIcon());
				m.setSubs(getMenus(nowRess, res.getResLevel() + 1, res.getId()));
				menus.add(m);
			}
		}
		return menus;
	}

	public List<Ress> getRess() {
		List<Ress> nowRess = new ArrayList<Ress>();
		try {
			String path = getSession().getServletContext().getRealPath("/");
			StringBuilder sb = new StringBuilder();
			sb.append(path);
			sb.append("WEB-INF");
			sb.append(File.separator);
			sb.append("classes");
			sb.append(File.separator);
			sb.append("menu.xls");
			List<Map<String, String>> map = POIUtil.readExcelKeyMap(sb.toString());
			for (Map<String, String> m : map) {
				Ress r = new Ress();
				r.setId(m.get("id"));
				r.setResName(m.get("resName"));
				r.setResUrl(m.get("resUrl"));
				r.setResIcon(m.get("resIcon"));
				r.setResPid(m.get("resPid"));
				r.setResLevel(Integer.parseInt(m.get("resLevel")));
				r.setResSort(Integer.parseInt(m.get("resSort")));
				nowRess.add(r);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return nowRess;
	}

	public HttpSession getSession() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		return attrs.getRequest().getSession();
	}

	public boolean isNotEmpty(String str) {
		if (str != null && str.length() > 0) {
			return true;
		}
		return false;
	}
}
